package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.statics.EnumUtils;
import com.sxbbc.common.core.dto.jz.FarmOrderOutVo;
import com.sxbbc.common.core.entity.jz.ChickenOrder;
import com.sxbbc.common.core.enums.FarmOrderStatusEnums;
import com.sxbbc.common.core.enums.OrderTypeEnums;
import com.sxbbc.common.core.enums.PayTypeEnums;
import com.sxbbc.common.core.mapper.jz.ChickenLevelMapper;
import com.sxbbc.common.core.mapper.jz.ChickenOrderMapper;
import com.sxbbc.common.core.service.IChickenOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 * 养鸡订单 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-15
 */
@Service
public class ChickenOrderServiceImpl extends ServiceImpl<ChickenOrderMapper, ChickenOrder> implements IChickenOrderService {
    @Autowired
    private ChickenOrderMapper chickenOrderMapper;
    @Autowired
    private ChickenLevelMapper chickenLevelMapper;

    public ChickenOrder getChickenOrder(Integer id) {
        return  chickenOrderMapper.getChickenOrder(id);
    }

    /**
     * 养鸡订单明细列表
     *
     * @param chickenOrder 查询条件
     * @return 分页对象
     * @author leehao
     */
    public IPage<FarmOrderOutVo> selectChickenOrderList(BasePage<ChickenOrder> page, ChickenOrder chickenOrder) {
        IPage<ChickenOrder> chickenOrderList = chickenOrderMapper.selectChickenOrderList(page, chickenOrder);
        IPage<FarmOrderOutVo> outPage=new BasePage<FarmOrderOutVo>();
        ArrayList<FarmOrderOutVo> outVos = new ArrayList<>();
        if (chickenOrderList.getSize() > 0) {
            for (ChickenOrder record : chickenOrderList.getRecords()) {
                record.setOrderTypeName(EnumUtils.getEnum(OrderTypeEnums.class, EnumUtils.GET_ID, record.getOrderType()).getName());
                record.setOrderStatusName(EnumUtils.getEnum(FarmOrderStatusEnums.class, EnumUtils.GET_ID, record.getOrderStatus()).getName());
                record.setPayTypeName(EnumUtils.getEnum(PayTypeEnums.class, EnumUtils.GET_ID, record.getPayType()).getName());
                FarmOrderOutVo vo = new FarmOrderOutVo();
                BeanUtils.copyProperties(record,vo);
                vo.setTitle(record.getLevel().toString()+"鸡舍"+record.getNumber()+"编号");
                vo.setQty(record.getBuyNum());
                long sub = DateUtil.getInstance().getDaySub(record.getCreateTime(),new Date());
                vo.setDay((int)sub+1);
                outVos.add(vo);
            }

            outPage.setCurrent(page.getCurrent());
            outPage.setRecords(outVos);
        }
        return outPage;
    }

    @Override
    public int updateDateByOrderCode(ChickenOrder order) {
        return chickenOrderMapper.updateDateByPayCode(order);
    }
}
